﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleRest.Core;
using SimpleRest.Core.Extensions.OAuth;

namespace SimpleRest.WebTest.Services
{
    [Service("oauth2/authorize")]
    public class AuthorizationService : AuthorizationServiceBase
    {
        public override string CreateAuthorizationCode(AuthorizationRequest authorizeRequest)
        {
            return base.CreateAuthorizationCode(authorizeRequest);
        }
        public override bool IsAuthorized(BearerToken bearerToken, string protectedResource, Core.Operation targetOperation)
        {
            if (string.IsNullOrEmpty(bearerToken.Value))
                return false;
            try
            {
                var token = AccessTokensService.AccessTokens.FirstOrDefault(d => d.Key.access_token == bearerToken.Value);

                if (token.Value <= DateTime.Now)
                {
                    AccessTokensService.AccessTokens.Remove(token.Key);
                    return false;
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
        public override bool IsAuthorized(MacToken token, string protectedResource, Core.Operation targetOperation)
        {
            if (token.id == "BAHAHAHAHAHA")
                return true;
            return false;
        }
    }
}